package cn.aresoft.excel.utils;

import cn.aresoft.configuration.PoiConfigProperties;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yangfeng
 * @description:
 * @date 2024/1/31 15:47
 */
@Component
public class ExcelStyleUtil {


    public static final String DATA_STYLE = "data_style";
    public static final String HEAD_STYLE = "head_style";
    public static final String TITLE_STYLE = "title_style";
    public static final String CELL_STYLE = "CELL_STYLE";
    public static final String GROUP_STYLE = "GROUP_STYLE";

    public static final Map<String,CellStyle> styles = new HashMap<>();
    @Autowired
    private PoiConfigProperties poiConfigProperties;
    /**
     * 创建表格样式
     * @return 样式列表
     */
    public Map<String, CellStyle> createStyles(Workbook book) {

        if(poiConfigProperties.getCellStyleSelfDefineHandler() != null){
           CellStyle dataCellStyle = poiConfigProperties.getCellStyleSelfDefineHandler().getDataCellStyle(book);
           styles.put(DATA_STYLE, dataCellStyle);
            CellStyle headCellStyle = poiConfigProperties.getCellStyleSelfDefineHandler().getHeadCellStyle(book);
            styles.put(HEAD_STYLE, headCellStyle);
            CellStyle groupCellStyle = poiConfigProperties.getCellStyleSelfDefineHandler().getGroupCellStyle(book);
            styles.put(GROUP_STYLE, groupCellStyle);
            CellStyle titleCellStyle = poiConfigProperties.getCellStyleSelfDefineHandler().getTitleCellStyle(book);
            styles.put(TITLE_STYLE, titleCellStyle);
        }else {
            CellStyle style = book.createCellStyle();
            style.setAlignment(HorizontalAlignment.CENTER);
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            Font titleFont = book.createFont();
            titleFont.setFontName("Arial");
            titleFont.setFontHeightInPoints((short) 16);
            titleFont.setBold(true);
            style.setFont(titleFont);
            styles.put(TITLE_STYLE, style);

            style = book.createCellStyle();
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            style.setBorderRight(BorderStyle.THIN);
            style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.index);
            style.setBorderLeft(BorderStyle.THIN);
            style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.index);
            style.setBorderTop(BorderStyle.THIN);
            style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.index);
            style.setBorderBottom(BorderStyle.THIN);
            style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.index);
            Font dataFont = book.createFont();
            dataFont.setFontName("Arial");
            dataFont.setFontHeightInPoints((short) 10);
            style.setFont(dataFont);
            style.setAlignment(HorizontalAlignment.CENTER);
            styles.put(DATA_STYLE, style);

            style = book.createCellStyle();
            style.cloneStyleFrom(styles.get(DATA_STYLE));
            styles.put(CELL_STYLE, style);

            style = book.createCellStyle();
            style.cloneStyleFrom(styles.get(DATA_STYLE));
    //		style.setWrapText(true);
            style.setAlignment(HorizontalAlignment.CENTER);
            style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.index);
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            Font headerFont = book.createFont();
            headerFont.setFontName("Arial");
            headerFont.setFontHeightInPoints((short) 10);
            headerFont.setBold(true);
            headerFont.setColor(IndexedColors.WHITE.index);
            style.setFont(headerFont);
            styles.put(HEAD_STYLE, style);
        }

        return styles;
    }

    /**
     * 获取单元格格式
     * @param book
     * @param styleName
     * @return
     */
    public CellStyle  getCellStyle(Workbook book,String styleName){
        CellStyle cellStyle = styles.get(styleName);
        if(cellStyle == null){
            createStyles(book);
        }
        cellStyle = styles.get(styleName);
        return cellStyle;
    }

}
